$(function () {


    var pageTable = function(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {

            str += '<tr data-id="' + list[i].id + '">' +
                '<td> <img width="100" src="' + list[i].cover + '"></td>' +
                '<td>' + list[i].url + '</td>' +
                '<td>' +
                '<a  href="#/banner/edit/'+ list[i].id +'" class="btn btn-primary btn-xs" >查看编辑</a> ' +
                '<a  class="btn btn-danger btn-xs btn-delete" >删除</a></td>' +
                '</tr>'
        }

        return str
    };




    //分页初始化
    var teacherPage = new PageCreate({
        url:'/banner/doPageQuery',
        container:$("#table tbody"),
        pagination:$("#page"),
        success:function (container,data) {
            container.html(pageTable(data.list))
        }
    });


    var init = function init() {

        teacherPage.init({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token')
        })

        $("#table tbody").on("click",".btn-delete",function () {

            var _id = $(this).parents('tr').attr("data-id")

            $.rootOpenDialog({
                title:"删除",
                btn:"确定",
                content:'<div style=" padding:0 15px; line-height: 40px">是否确定删除</div>',
                yes:function (index,lay) {
                    $.restPost('/banner/doRemove/' + _id,{
                        token: window.localStorage.getItem('token')
                    }).done(function(mess,data){
                        layer.close(index)
                        init();
                        $.rootSuccessDialog(mess,1500);
                    }).fail(function(mess){
                        $.rootErrorDialog(mess,1500);
                    })

                }
            })
        })
    }


    init()
});
